package com.uroad.lib.widget.photopicker.widget;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.uroad.lib.R;
import com.uroad.lib.imageloader.ImageLoaderHelper;
import com.uroad.lib.widget.photopicker.PhotoPickUtils;
import com.uroad.lib.widget.photopicker.PhotoPreview;

import java.io.File;
import java.util.ArrayList;

public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.PhotoViewHolder> {

    //图片质量，一共三个属性，标清，高清，无图。目前只考虑无图和有图两个属性
    public int quality;

    private ArrayList<String> photoPaths;
    private LayoutInflater inflater;
    private Context mContext;
    private ImageView ivAdd;


    public void setAction(@MultiPickResultView.MultiPicAction int action) {
        this.action = action;
    }

    public void setSize(@MultiPickResultView.MultiPicAction int size) {
        this.size = size;
    }

    public void setDefultPic(int defultPic) {
        this.defultPic = defultPic;
    }

    private int action, size, defultPic = R.drawable.ic_picker_pic_default;

    public PhotoAdapter(Context mContext, ArrayList<String> photoPaths) {
        this.photoPaths = photoPaths;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
        padding = dip2Px(8);
    }

    public void add(ArrayList<String> photoPaths) {
        if (photoPaths != null && photoPaths.size() > 0) {
            this.photoPaths.addAll(photoPaths);
            notifyDataSetChanged();
        }
    }

    public void refresh(ArrayList<String> photoPaths) {
        this.photoPaths.clear();
        if (photoPaths != null && photoPaths.size() > 0) {
            this.photoPaths.addAll(photoPaths);
        }
        notifyDataSetChanged();
    }


    @Override
    public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.picker_item_photo, parent, false);
        return new PhotoViewHolder(itemView);
    }

    public int dip2Px(int dip) {
        // px/dip = density;
        float density = mContext.getResources().getDisplayMetrics().density;
        int px = (int) (dip * density + .5f);
        return px;
    }

    int padding;

    @Override
    public void onBindViewHolder(final PhotoViewHolder holder, final int position) {

        if (action == MultiPickResultView.ACTION_SELECT) {
            holder.ivPhoto.setPadding(padding, padding, padding, padding);
            if (position == getItemCount() - 1 && photoPaths.size() < size) {//最后一个始终是+号，点击能够跳去添加图片
                Glide.with(mContext)
                        .load("")
                        .centerCrop()
                        .thumbnail(0.1f)
                        .placeholder(defultPic)
                        .error(defultPic)
                        .into(holder.ivPhoto);
                holder.ivPhoto.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PhotoPickUtils.startPick((Activity) mContext, photoPaths, size);
                    }
                });
                ivAdd = holder.ivPhoto;
                holder.deleteBtn.setVisibility(View.GONE);
            } else {
                String str = photoPaths.get(position);
                Uri uri = Uri.fromFile(new File(photoPaths.get(position)));
                Glide.with(mContext)
                        .load(uri)
                        .centerCrop()
                        .thumbnail(0.1f)
                        // .bitmapTransform(new RoundedCornersTransformation(mContext,6,0))
                        .placeholder(R.drawable.bg_rectangle_picker_default_weixin)
                        .error(R.drawable.ic_picker_broken_image_black)
                        .into(holder.ivPhoto);

                holder.deleteBtn.setVisibility(View.VISIBLE);
                holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        photoPaths.remove(position);
                        notifyDataSetChanged();
                    }
                });

                holder.ivPhoto.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PhotoPreview.builder()
                                .setPhotos(photoPaths)
                                .setAction(action)
                                .setCurrentItem(position)
                                .start((Activity) mContext);
                    }
                });
            }
        } else if (action == MultiPickResultView.ACTION_ONLY_SHOW) {
            if(quality == ImageLoaderHelper.PIC_QUALITY_NO_PIC){
                holder.ivPhoto.setImageResource(R.drawable.ic_picker_broken_image_black);
                return;
            }
            //Uri uri = Uri.fromFile(new File(photoPaths.get(position)));
            //Uri uri = Uri.parse(photoPaths.get(position));
            Glide.with(mContext)
                    .load(photoPaths.get(position))
                    .centerCrop()
                    .thumbnail(0.1f)
                    // .bitmapTransform(new RoundedCornersTransformation(mContext,4,0))
                    .placeholder(R.drawable.bg_rectangle_picker_default_weixin)
                    .error(R.drawable.ic_picker_broken_image_black)
                    .into(holder.ivPhoto);


            holder.ivPhoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    PhotoPreview.builder()
                            .setPhotos(photoPaths)
                            .setAction(action)
                            .setCurrentItem(position)
                            .start((Activity) mContext);
                }
            });
        }
    }


    @Override
    public int getItemCount() {
        return photoPaths.size() == size ? photoPaths.size() : photoPaths.size() + 1;
    }

    //提供可让外界调用的添加图片的方法
    public void addPic() {
        if (ivAdd != null) {
            if (photoPaths.size() < size) {
                ivAdd.performClick();
            } else {
                //无法在添加图片了
            }
        }
    }


    public static class PhotoViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivPhoto;
        private View vSelected;
        public View cover;
        public View deleteBtn;

        public PhotoViewHolder(View itemView) {
            super(itemView);
            ivPhoto = (ImageView) itemView.findViewById(R.id.iv_photo);
            vSelected = itemView.findViewById(R.id.v_selected);
            vSelected.setVisibility(View.GONE);
            cover = itemView.findViewById(R.id.cover);
            cover.setVisibility(View.GONE);
            deleteBtn = itemView.findViewById(R.id.v_delete);
            deleteBtn.setVisibility(View.GONE);
        }
    }


}
